//
//  BGBaseNavigationController.m
//  CappingNews
//
//  Created by wang on 15/6/9.
//  Copyright (c) 2015年 Bitenergy. All rights reserved.
//

#import "BGBaseNavigationController.h"
#import "UIImage+Extension.h"

typedef enum : NSUInteger {
    BGBarThemePlanHome,//首页专用  不能透明,透明会frame变动
    BGBarThemePlanNavColorAphla,//详情需要透明
    BGBarThemePlanWhite,//白色
} BGBarThemePlan;

@interface BGBaseNavigationController () <UINavigationControllerDelegate>

@end

@implementation BGBaseNavigationController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //清空弹出手势的代理，就可以恢复弹出手势
    self.interactivePopGestureRecognizer.delegate = nil;
    self.delegate = self;
}

//- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    [self setAppearancePlan:[self barThemePlanA:viewController]];
//}

/**
 *  能拦截所有push进来的子控制器
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // 如果现在push的不是栈底控制器(最先push进来的那个控制器)
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        // 设置导航栏按钮
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_backArrow_normal"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];//btn_back_normal
    }
    //设置push的颜色
//    viewController.view.backgroundColor = [UIColor whiteColor];
    [super pushViewController:viewController animated:animated];
}

//- (BGBarThemePlan )barThemePlanA:(UIViewController *)controller {
//    
//    if ([controller isKindOfClass:[BGMainViewController class]]) {
//        return BGBarThemePlanHome;
//    }
//    
//    NSArray *planAControllerClassStringArray = @[NSStringFromClass([BGPersonCenterController class]),NSStringFromClass([BGProductDetailController class])];
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", NSStringFromClass([controller class])];
//    if ([planAControllerClassStringArray filteredArrayUsingPredicate:predicate].count > 0) {
//        return BGBarThemePlanNavColorAphla;
//    }
//    return BGBarThemePlanWhite;
//}

- (void)setAppearancePlan:(BGBarThemePlan)plan {
    if (plan == BGBarThemePlanHome) {
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
         [self setAppearance:[UIColor navTopColor] titleColor:[UIColor whiteColor] tintColor:[UIColor whiteColor]];
    }else if (plan == BGBarThemePlanNavColorAphla) {
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        [self setAppearance:[UIColor navTopAlphaColor] titleColor:[UIColor whiteColor] tintColor:[UIColor navTopColor]];
    }else {//BGBarThemePlanWhite
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
        [self setAppearance:[UIColor whiteColor] titleColor:[UIColor blackColor] tintColor:[UIColor navTopColor]];
    }
}

/**
 *  设置导航栏样式
 */
- (void)setAppearance:(UIColor *)imageColor titleColor:(UIColor *)titleColor tintColor:(UIColor *)tintColor
{
    UINavigationBar *appearance = self.navigationBar;
    [appearance setBackgroundImage:[UIImage imageWithColor:imageColor] forBarMetrics:UIBarMetricsDefault];
    
    NSDictionary *textAttrs = @{ NSFontAttributeName: [UIFont systemFontOfSize:19] ,
                                 NSForegroundColorAttributeName: titleColor
                                 };
    // 导航栏标题的大小和颜色
    [appearance setTitleTextAttributes:textAttrs];
    
    // 左右字体大小和颜色
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:textAttrs forState:(UIControlStateNormal)];
    // 统一的渲染颜色
    [appearance setTintColor:tintColor];
}

/**
 *  返回上一级
 */
- (void)back
{
    // 这里用的是self, 因为self就是当前正在使用的导航控制器
    [self popViewControllerAnimated:YES];
}




@end
